// pages/planList/planList.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectId:'',
    year:'',
    tabsIndex:2,
    list:[],
    month:'',
    detail:{},
    key:0,
    advanceList:[],
    diffiValue:''
  },

  //跳转申报操作页面
  addDeclare:function(){
    var list = this.data.list;
    var key = this.data.key;
    var month = list[key].month; //月份
    var projectId = this.data.projectId;  //项目id
    var year = this.data.year; //年份
    wx.navigateTo({  url: `/pages/addDeclare/addDeclare?id=`+projectId+'&year='+ year+'&month='+month});
  },
  
  onTab:function(e){
    var projectId = this.data.projectId;
    var tabsIndex = e.currentTarget.dataset.index;
    wx.navigateTo({  url: `/pages/project/project?id=`+projectId+'&tabsIndex='+ tabsIndex});
  },

  //月份选择
  bindPickerChange:function(t){
    var list = this.data.list;
    if(t.detail == undefined){
      var key = t;
    }else{
      var key = t.detail.value;
    }
    var detail = list[key];
    var month = detail.month;

    this.setData({
      month:month,
      detail:detail,
      key:key
    });

  },
  
  //获取每月数据
  planList:function(month=0){
    var projectId = this.data.projectId;
    var year = this.data.year;

    let params = {
      id: projectId,
      year: year,
    };

    var self = this;
    app.request('post', '/api/Project/planList', params).then(res => {
        self.setData({
          list:res.data.list,
          leading_cadre:res.data.leading_cadre,
          advanceList:res.data.advanceList,
        });
       
        this.bindPickerChange(month);
        wx.setNavigationBarTitle({ title: res.data.title});

    }).catch(err => console.log(err));

   },


   //更新数据
   bindAdvance:function(t){
    var advanceList = this.data.advanceList;
    var key1 = t.detail.value;
    var detail1 = advanceList[key1];
    var list = this.data.list;
    var key = this.data.key;
    var detail = list[key];
    var projectId = this.data.projectId;
    var year = this.data.year;
    var month = detail.month;
    var advance_id = detail1.id;
    
    let params = {
      id: projectId,
      year: year,
      month:month,
      advance_id:advance_id
    };
    var self = this;
    app.request('post', '/api/Project/addPlanList', params).then(res => {
        self.planList(key);
    }).catch(err => console.log(err));
   },


    /** 基本信息 - 修改 节点计划 */
    onUpdateDiffi(e){
      var list = this.data.list;
      var key = this.data.key;
      var detail = list[key];
      this.setData({
        diffiValue:detail.content
      }),
      this.diffiPopup.showPopup();
    },

    /**  节点计划 - 弹出层输入*/
    onDiffiInput(e){
      this.setData({ diffiValue: e.detail });
    },

    //节点计划提交
    onDiffiSubmit(e){
      var list = this.data.list;
      var key = this.data.key;
      var detail = list[key];
      var projectId = this.data.projectId;
      var year = this.data.year;
      var month = detail.month;
      var content =this.data.diffiValue;
      let params = {
        id: projectId,
        year: year,
        month:month,
        content:content
      };
      var self = this;
      app.request('post', '/api/Project/addPlanList', params).then(res => {
          self.planList(key);
      }).catch(err => console.log(err));

      this.diffiPopup.hidePopup();
    },


  /** 基本信息 - 节点计划 - 弹出层关闭*/
  onDiffiClose(){
      this.diffiPopup.hidePopup();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      projectId:options.id,
      year:options.year
    });

    if(options.month==undefined){
      var curDate = new Date();
      var month = curDate.getMonth();
    }else{
      var month = options.month;
    }
    this.planList(month);
  },


  //图片渲染
  previewImg:function(e){
      let that = this;
      var key = this.data.key;
      wx.previewImage({
        current: e.currentTarget.dataset.src,   //当前图片地址
        urls: that.data.list[key].images,        //所有要预览的图片的地址集合 数组形式
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.diffiPopup = this.selectComponent('#diffiPopup')
    console.log(this.diffiPopup);
    console.log('zxd');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})